﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using SpLocalizer.Common.DbApi;
using SpLocalizer.ContentSourceProviders.SpTaxonomy;
using Microsoft.Practices.Unity;
using Microsoft.SharePoint.Taxonomy;

namespace SpLocalizer.ContentSourceProviders
{
    public class SpTaxonomyTranslationProject : AbstractTranslationProject, IDisposable
    {


        private SPSite _site = null;
        /// <summary>
        /// Local SharePoint Site project handle
        /// </summary>
        private SPSite Site
        {
            get
            {
                if (this._site == null)
                {
                    this._site = new SPSite(this.ConnectionString);
                }
                return this._site;
            }
        }

        private TaxonomySession _session = null;
        private TaxonomySession Session{
            get{
                if (this._session == null){
                    this._session = new TaxonomySession(this.Site); 
                }
                return this._session;
            }
        }

        public override void PerformExportDone() {
            // Resync the taxonomy hidden list to make sure it is update-to-date
            TaxonomySession.SyncHiddenList(this.Site);
        }

        protected override void Bind(bool IncludeDescendants)
        {            
                     
            TermStoreCollection TermStoreColl = this.Session.TermStores;
            this.ProjectTitle = String.Format("Taxonomy for site {0}", this.ConnectionString);
            foreach (TermStore TermStore in TermStoreColl)
            {
                if (TermStore.DoesUserHavePermissions(TaxonomyRights.All))
                {
                    throw new ApplicationException(String.Format("You don't have TaxonomyRights.All permissions for Termstore {0}", TermStore.Name));
                }                

                this._ContentData.AddRange(SpTermSetContent.LoadAllFromTermStore(TermStore));
            }

        }

        protected override AbstractContent LoadContent(LocalizableContent content)
        {
            SpTermSetContent TermSetContent = ServiceContainer.Resolve<AbstractContent>(content.ContentSourceProvider) as SpTermSetContent;
            if (TermSetContent != null) // We have only ione content provider 
            {
                TermSetContent.Bind(this.Session, content.ContentId, content.ContentTitle, content.ContentAddress);
                return TermSetContent;
            }
            return null;
        }

        protected override void RegisterContentProviderTypes()
        {
            ServiceContainer.Container.RegisterType<AbstractContent, SpTermSetContent>(typeof(SpTermSetContent).Name);
            ServiceContainer.Container.RegisterType<BaseTranslationTask, SpTermLocalizationTask>(typeof(SpTermLocalizationTask).Name);
        }

        public void Dispose()
        {
            if (this._site != null)
            {
                this._site.Close();
                this._site = null;
            }
        }
    }
}
